
from yacf.Frontend.C99 import c99_lexer 
from ply.lex import TOKEN
class OmpLexer(object):

    @staticmethod
    def get_reserved():
        reserved = {
		'pragma'    : 'PRAGMA',
		# llc v2 syntax
		'llc'   	: 'LLC',
		# llc yacf-frangollo
		'context'	: 'REGION',
		'task'  	: 'TASK',
		'name'		: 'NAME',
		'concycle' 	: 'CONCYCLE',
		'in' 		: 'IN',
		'out'		: 'OUT',
		'inout'		: 'INOUT',
		# ----- Loop Transformations ------
		'nested_for': 'NESTED_FOR',
		'interchange': 'INTERCHANGE',
		'unroll'	: 'UNROLL',
		'unswitch'	: 'UNSWITCH',
		'tiling'	: 'TILING',
		# -------- omp ---------
		'omp'		: 'OMP',
		'parallel'	: 'PARALLEL',
		'sections'	: 'SECTIONS',
		# ------ clauses --------
		'shared'	: 'SHARED',
		'private'	: 'PRIVATE',
		'reduction'	: 'REDUCTION',
		'nowait'	: 'NOWAIT',
		'threadprivate': 'THREADPRIVATE',
		'copyin'	: 'COPYIN',
		'schedule'	: 'SCHEDULE',
		'omp_schedule_kind' : 'OMP_SCHEDULE_KIND',
		'master' 	: 'OMP_MASTER',
		'critical'	: 'OMP_CRITICAL',
		'atomic'	: 'OMP_ATOMIC',
		'barrier'	: 'OMP_BARRIER',
		'flush'		: 'OMP_FLUSH',
		'single'	: 'OMP_SINGLE',
		'firstprivate': 'FIRSTPRIVATE',
		'collapse'	: 'COLLAPSE',
		'none'		: 'NONE',
		'result'	: 'RESULT',
		'nc_result' : 'NCRESULT',
		'num_threads': 'NUM_THREADS',
		'lastprivate': 'LASTPRIVATE',
		# ------- new clauses --------
		# From  "A proposal to extend the OpenMP Tasking Model for Heterogeneous Architectures" (2009)
		'device'	: 'DEVICE',
		'target'	: 'TARGET',
		'copy_in'	: 'COPY_IN',
		'copy_out'	: 'COPY_OUT',
		# ----- CELL syntax ---------
		'cell'		: 'CELL',
		# --------- CUDA -----------
		'cuda'		: 'CUDA',
		# --------- FPGA -----------
		'fpga'		: 'FPGA',
		# --------- MPI -----------
		'mpi'		: 'MPI',
		'hybrid'	: 'HYBRID',
		'smp' 		: 'SMP',
        # -------- QUICK HACK FOR CUDA/GMAC, CLEAN ME ------
     
        'default'   : 'OMP_DEFAULT',
        'for'		: 'OMP_FOR',
	    #'#'			: 'OMP_PPHASH',
		'omp'		: 'OMP_OMP'

	}
        return reserved   
    #allreserved = reserved.copy()
    #allreserved.update(c99_lexer.C99Lexer.keyword_map)

    @staticmethod
    def get_tokens():
        tokens = list(OmpLexer.get_reserved().values()) 
        return tokens

    def t_directive_ID(self, t):
        r'[a-zA-Z_][0-9a-zA-Z_]*'
        t.type = OmpLexer.get_reserved().get(t.value, 'ID')
        #t.lexer.begin('INITIAL')
        return t

    @staticmethod
    def get_states():
        states = tuple()
        return states

if __name__ == '__main__':
    filename = '../testsC99/5_omp.c'
    text = open(filename).read()
    
    def errfoo(msg, a, b):
        printme(msg)
        sys.exit()
    
    def typelookup(namd):
        return False
    
    omplex = OmpLexer(errfoo, typelookup)
    omplex.build()
    omplex.input(text)
    
    while 1:
        tok = omplex.token()
        if not tok: 
			break
        print tok
